#include <iostream>
#include <cstdio>
#include <pthread.h>
#include <string>
#include <unistd.h>
#define NUM 10
using namespace std;
/*
创建10个线程
*/

class ThreadData
{
public:
    pthread_t _tid;
    char _namebuffer[64];
};
void *start_routine(void *arg)
{
    ThreadData* data = static_cast<ThreadData*>(arg); // 一种安全的转换方式
    while (true)
    {
        sleep(1);
        cout << "子线程创建成功,名字是:" <<data->_namebuffer  << ", 线程的id是: 0x" << hex << pthread_self() << dec << endl;
    }
    return nullptr;
}
int main()
{
    for (int i = 0; i < NUM; i++)
    {
        ThreadData* thread_data[NUM];
        // pthread_t pid;
        //    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
        // char namebuffer[64];
        ThreadData* cnt = new ThreadData();
        snprintf(cnt->_namebuffer, sizeof cnt->_namebuffer, "zk创建的线程%d", i);
        // cnt->_namebuffer =namebuffer;
        // cout<<namebuffer<<endl;
        pthread_create(&cnt->_tid, NULL, start_routine, cnt);
    }
    while (true)
    {

        cout << "new thread create success,now is main thread: id: " << hex << pthread_self() << dec << endl;
        sleep(1);
    }

    return 0;
}